<form action='controller.php' method='POST'> <-- in view_startpage.php --> <input type='hidden' name='page' value='StartPage'> <input type='hidden' name='command' value='SignIn'> <input type='text' name='username'> <input type='password' name='password'> <input type='submit'> </form>
<?php //---- Case 1 ---- if (empty($_POST['page']) { // What does this mean? include("view_startpage.php"); // Logically the code in "view_..." will be copied to this position. exit(); } //---- Case 2 ---- $page = $_POST['page']; if ($page == 'StartPage') { $command = $_POST['command']; // In view_startpage.php, an input in a form, of which type is 'hidden' and name is 'command'. switch($command) { case 'SignIn': ... exit(); // or break when there is something more to do after switch case 'Join': // or 'SignUp; ... exit(); case 'ForgotPassword': ... exit(); default: ... exit(); } } //---- Case 3 ---- else if ($page == 'MainPage') { ... exit(); } //---- Case 4 ---- else { ... exit(); } ?>
include()
and require()
are like the import statement in Java and the include statement in C.include()
and require()
different?include_once()
and require_once()
.<!-- controller.php --> <?php ... $display_modal_window = 'signin'; include ('view_startpage.php'); // When controller.php is executed, // this will be replaced by the code in 'view_startpage.php'. ... ?> --------------------------------------- <!-- view_startpage.php --> ... window.addEventListener('load', function() { ...; <?php if ($display_modal_window == 'signin') echo 'show_signin();'; // send JavaScript code back to the client ?> }); ...
$display_modal_window
is used to pass information to the code in vew_startpage.php.
After the inclusion, controller.php becomes like
<!-- controller.php -->
<?
...
$display_modal_window = 'signin';
?>
...
window.addEventListener('load', function() {
...;
<?php
if ($display_modal_window == 'signin')
echo 'show_signin();'; // echo JavaScript code
?>
});
...
<?php
...
?>
$display_modal_window = 'signin'
?... window.addEventListener('load', function() { ...; show_signin(); ... }); ...
<?php // Case 1: When no page value is sent from the client if (empty($_POST['page'])) { // That is, controller.php is the URL of TRUQA. http://cs.tru.ca/.../controller.php // No POST or GET data comes. // You may use if (!isset($_POST['page'])) instead of empty(...). $display_modal_window = 'none'; // This variable will be used in 'view_startpage.php'. // It will display the start page without any modal window, i.e., no SignIn box, no Join box, ... include ('view_startpage.php'); // The user will see StartPage without any modal window on. exit(); } require('model.php'); // This file includes some routines to access DB. ... // Case 2: When a command comes from StartPage if ($_POST['page'] == 'StartPage') { $command = $_POST['command']; switch($command) { // When a command is sent from the client through the SignIn modal window case 'SignIn': // With username and password if (there is an error in username and password) { $error_msg_username = '* Wrong username, or'; $error_msg_password = '* Wrong password'; // Set an error message into a variable. // This variable will used in the form in 'view_startpage.php'. $display_modal_window = 'signin'; // It will display the start page with the SignIn box. // This variable will be used in 'view_startpage.php'. include('view_startpage.php'); // The user will see StartPage with the SignIn modal window on. } else include ('view_mainpage.php'); // The user will see MainPage. exit(); case 'Join': // or 'SignUp' with username, password, email, some other information ... exit(); ... } } // Case 3: When a command comes from 'MainPage' else if ($_POST['page'] == 'MainPage'){ ... } ?>
<!DOCTYPE html> ... <body> ... <div id='signin-box' style='display:none; ...'> <form id='signin-form' method='POST' action='controller.php'> <input type='???' name='page' value='StartPage'><br> <input type='???' name='command' value='SignIn'><br> Username: <input ...> <?php if (!empty($error_msg_username)) echo $error_msg_username; // Display error message if there is ?><br> Password: <input ...> <?php if (???) echo $error_msg_password; ?><br> <input type='submit'> </form> </div> ... <script> function show_signin() { ???? } ... <?php // Can you study this code carefully? if (??? == 'none') // The variable, $display_modal_window, is set in controller.php before include('view_startpage.php'); ; else if (??? == 'signin') echo 'show_signin();'; else if ... ?> ... </script> ... </body>
$display_modal_window
is 'none'
, the following code is sent to the client.
Note that the code to invoke show_signin()
is not included.
<!DOCTYPE html> ... <body> ... <div id='signin-box' style='display:none; ...'> <form id='signin-form' method='POST' action='controller.php'> <input type='???' name='page' value='StartPage'><br> <input type='???' name='command' value='SignIn'><br> Username: <input ...> <br> Password: <input ...> <br> <input type='submit'> </form> </div> ... <script> function show_signin() { ???? } ... </script> ... </body>
$display_modal_window
is 'signin'
, $error_msg_username
has '* Wrong username, or'
, and $error_msg_password
has '$ Wrong password'
, the code sent to the client is
<!DOCTYPE html> ... <body> ... <div id='signin-box' style='display:none; ...'> <form id='signin-form' method='POST' action='controller.php'> <input type='???' name='page' value='StartPage'><br> <input type='???' name='command' value='SignIn'><br> Username: <input ...> * Wrong username, or<br> Password: <input ...> * Wrong password<br> ... </form> </div> ... <script> function show_signin() { ???? } ... show_signin(); ... </script> ... </body>
$display_modal_window
as well.